package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Order;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//订单模块
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    //用户下单
    @PostMapping("/order/submit")
    public ResultInfo submit(@RequestBody Order order) {//接受参数
        //调用service生成订单
        orderService.submit(order);
        //返回成功
        return ResultInfo.success();
    }

    //最新订单查询
    @GetMapping("/order/userPage")
    public ResultInfo findByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
        //调用service
        Page<Order> orderPage = orderService.findByPage(page, pageSize);
        //返回结果
        return ResultInfo.success(orderPage);
    }

    //退出登录
    @PostMapping("/user/logout")
    public ResultInfo logout() {
        //调用service
        userService.logout();
        //fanhui
        return ResultInfo.success();
    }

    //后端订单查询
    @PostMapping("/order/page")
    public ResultInfo findToPage(@PathVariable("page") Integer pageNum,
                                 @PathVariable("pageSize") Integer pageSize,
                                 Long orderNumber, String beginTime, String endTime) {
     Page<Order> page=   orderService.findToPage(pageNum, pageSize, orderNumber, beginTime, endTime);
     //返回结果
        return ResultInfo.success(page);
    }
}
